﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Licence;
using CaseManagerLibrary.Log;
using CaseManagerLibrary.Models;
using CaseManagerLibrary.Socket;
using PSDClientService;
using PSDClientService.SystemSettings;
using PSDLib;
using PSDLib.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDDeviceSNTool
{
    public partial class FrmSN : Form
    {
        public FrmSN()
        {
            InitializeComponent();

            StartPosition = FormStartPosition.CenterScreen;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.txtSN.Text = DeviceUUID.GetUUID();

            this.txtCpuid.Text = DeviceUUID.cpuId();

            this.txtBiosid.Text = DeviceUUID.biosId();

            this.txtBaseid.Text = DeviceUUID.baseId();

            this.txtMacid.Text = DeviceUUID.macId();

            RegistDevice();
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Clipboard.SetText(this.txtSN.Text.Trim());

            MessageBox.Show("PSD设备序列号已复制！");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        ClientServiceContext context;
        
        private void RegistDevice()
        {
            AppSettings.Setting.Update();

            context = new ClientServiceContext();
            context.Logger = new ConsoleLog();
            var device = context.DeviceManager.GetDeviceRegistInfo(this.txtSN.Text.Trim());

            if (device != null)
            {
                context.Device = device;

                var code = context.DeviceManager.CheckLicence();

                if (code == CaseManagerLibrary.Licence.LicenceCheckCode.Success)
                {
                    lblLicence.Text = "设备已授权";
                    lblLicence.ForeColor = Color.Green;
                    lblLicence.Tag = 1;
                }
                else
                {
                    lblLicence.Text = "设备未授权";
                    lblLicence.ForeColor = Color.Red;
                    lblLicence.Tag = 0;
                }
            }
        }

        private void lblLicence_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(lblLicence.Tag) != 1)
            {
                var ofg = new OpenFileDialog();
                ofg.Filter = "*.pal|*.pal";

                if (ofg.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        var licence = LicenceManager.GetLicenceContentFromFile(ofg.FileName, this.txtSN.Text.Trim());

                        if (licence != null)
                        {
                            var code = LicenceManager.Check(licence, this.txtSN.Text.Trim(), LicenceDaysFrom.Ignore);

                            if (code == LicenceCheckCode.Success)
                            {
                                if (context.Device == null) {
                                   context.Device = context.DeviceManager.GetDeviceRegistInfo(this.txtSN.Text.Trim());
                                }
                                context.Device.device_key = System.IO.File.ReadAllText(ofg.FileName);
                                context.Device.state = 1;

                                var result = context.DeviceManager.RegistDevice(context.Device);

                                if (result.success)
                                {
                                    this.lblLicence.Text = "设备已授权";
                                    this.lblLicence.ForeColor = Color.Green;
                                    lblLicence.Tag = 1;
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        var sw = System.IO.File.AppendText(System.IO.Path.Combine(Application.StartupPath, "sn.log"));
                        sw.WriteLine(ex.Message);
                        sw.WriteLine(ex.StackTrace);
                        sw.Close();
                    }
                }
            }
        }
    }
}
